package com.hejr.controller;

import com.hejr.common.CommonResultObject;
import com.hejr.entity.User;
import com.hejr.service.IUserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: hejr
 * @desc: 用户控制层
 * @date: 2019/4/8 13:33
 */
@RestController
@RequestMapping("/user")
@Data
public class UserController {

    @Autowired
    private IUserService userService;


    /**
     * 主库新增用户
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("addUserToMaster")
    public Object addUserToMaster(String name, Integer age){
        CommonResultObject resultObject = new CommonResultObject();
        try {
            User userMaster = User.builder().name(name).age(age).build();
            resultObject.setResult(userService.addUserToMaster(userMaster));
            resultObject.setCode(200);
            resultObject.setMessage("主库新增用户成功");
        } catch (Exception e) {
            resultObject.setCode(-1);
            resultObject.setMessage("主库新增用户失败");
            e.printStackTrace();
        }
        return resultObject;
    }

    /**
     * 从库新增用户
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("addUserToSlave")
    public Object addUserToSlave(String name, Integer age){
        CommonResultObject resultObject = new CommonResultObject();
        try {
            User userSlave = User.builder().name(name).age(age).build();
            resultObject.setResult(userService.addUserToSlave(userSlave));
            resultObject.setCode(200);
            resultObject.setMessage("从库新增用户成功");
        } catch (Exception e) {
            resultObject.setCode(-1);
            resultObject.setMessage("从库新增用户失败");
            e.printStackTrace();
        }
        return resultObject;
    }

    /**
     * 获取主库用户列表
     * @return
     */
    @RequestMapping("findMasterUserList")
    public Object findMasterUserList(){
        CommonResultObject resultObject = new CommonResultObject();
        try {
            resultObject.setResult(userService.findMasterUserList());
            resultObject.setCode(200);
            resultObject.setMessage("获取主库用户列表成功");
        } catch (Exception e) {
            resultObject.setCode(-1);
            resultObject.setMessage("获取主库用户列表失败");
            e.printStackTrace();
        }
        return resultObject;
    }



    /**
     * 获取从库用户列表
     * @return
     */
    @RequestMapping("findSlaveUserList")
    public Object findSlaveUserList(){
        CommonResultObject resultObject = new CommonResultObject();
        try {
            resultObject.setResult(userService.findSlaveUserList());
            resultObject.setCode(200);
            resultObject.setMessage("获取从库用户列表成功");
        } catch (Exception e) {
            resultObject.setCode(-1);
            resultObject.setMessage("获取从库用户列表失败");
            e.printStackTrace();
        }
        return resultObject;
    }


}
